#!/usr/bin/env bash
ShellDir=${JD_DIR:-$(cd $(dirname $0); pwd)}
logDir=$ShellDir/logs
export logDir
export ShellDir
export JD_COOKIE=''
export PUSH_KEY=''

export JOY_FEED_COUNT=40
export MARKET_COIN_TO_BEANS=20
export ENABLE_AUTO_HELP=true
export DEFAULT_LIST_FILE=crontab_list.sh
export CUSTOM_LIST_MERGE_TYPE=append
export REPO_URL=git@gitee.com:lxk0301/jd_scripts.git
export REPO_BRANCH=master
export FRUIT_BEAN_CARD="true"
export JD_JOY_REWARD_NAME="500"
export FACTORAY_WANTPRODUCT_NAME="刷"
export JXNC_NOTIFY_LEVEL="3"

MY_GIT=~/storage/external-1/myscripts
RANDOM_DELAY_MAX=30
param=$1
if [ -f $ShellDir/env.sh ] ; then
. $ShellDir/env.sh
fi

if [ "$HOSTNAME" == "OpenWrt" ]; then
export PATH=/mnt/sda4/opkg/usr/bin/:/mnt/sda1/opkg/usr/lib/git-core:$PATH
export LD_LIBRARY_PATH=/mnt/sda1/opkg/usr/lib/perl5/5.28/CORE/:/mnt/sda1/opkg/usr/lib/git-core:$LD_LIBRARY_PATH
fi

function auto_help(){
    #sed -i 's/^logDir=/#logDir=/g' $ShellDir/docker/auto_help.sh 
    if [ "$param" == "collect" ]; then
    . $ShellDir/docker/auto_help.sh collect
    else
    . $ShellDir/docker/auto_help.sh export > $logDir/auto_help_export.log
    fi
    #sed -i 's/^#logDir=/logDir=/g' $ShellDir/docker/auto_help.sh 
}

function run_js(){
	if [ -n "$ANDROID_ROOT" ] && [ -f $ShellDir/../not_run_js ]; then
		echo 'go back return'
		return
	fi
    auto_help
    export
    sleep $((RANDOM % $RANDOM_DELAY_MAX))
    node $param
}

function run_entrypoint(){
    shellenv=sh
    cd $ShellDir
    if [ "$HOSTNAME" == "OpenWrt" ]; then
        wget https://gitee.com/makeavailable/scripts/repository/archive/master.zip
        unzip -o master.zip -d ..
        rm master.zip
        export JD_COOKIE2=${JD_COOKIE//&/\\&}
        sed -i "s|^export JD_COOKIE=''|export JD_COOKIE='$JD_COOKIE2'|g" ajd.sh
        sed -i "s|^export PUSH_KEY=''|export PUSH_KEY='$PUSH_KEY'|g" ajd.sh
    else
        git remote set-url origin $REPO_URL
        git reset --hard
        echo "git pull拉取最新代码..."
        git -C $ShellDir pull --rebase
    fi
    if [ -n "$ANDROID_ROOT" ]; then

        export CUSTOM_LIST_FILE=extra_task.sh
        export CUSTOM_LIST_MERGE_TYPE=append

        echo push code
        rm README.md
        if [ ! -d $MY_GIT ]; then
        git clone --progress -v "git@makeavailable:makeavailable/scripts.git" "$MY_GIT"
        fi
        cd $MY_GIT
        git reset --hard
        git -C $MY_GIT pull --rebase
        cd $ShellDir
        for d in `ls -F|grep '/$'|grep -v node_modules`
        do
            echo cp -fr ./$d $MY_GIT/
            cp -fr ./$d $MY_GIT/
        done
        echo cp *.* $MY_GIT/
        cp *.* $MY_GIT/
        cd $MY_GIT
        sed -i "s|^export JD_COOKIE=.*|export JD_COOKIE=''|g" ajd.sh
        sed -i "s|^export PUSH_KEY=.*|export PUSH_KEY=''|g" ajd.sh
        git add .
        git commit -m "同步大佬的代码"
        git push
        cd $ShellDir
    fi

    for file in `ls $ShellDir/docker/|grep -E ".sh|.js"` #注意此处这是两个反引号，表示运行系统命令
    do
        if [ -f $ShellDir/docker/$file ] #注意此处之间一定要加上空格，否则会报错
        then
			#echo sed -i "s|/scripts|$ShellDir|g" $ShellDir/docker/$file
            sed -i "s|/scripts|$ShellDir|g" $ShellDir/docker/$file
            if [ -n "$ANDROID_ROOT" ] || [ "$HOSTNAME" == "OpenWrt" ]; then
               shellenv=bash
                sed -i "s|sh -x|bash -x|g" $ShellDir/docker/$file
                sed -i "s|sh |bash |g" $ShellDir/docker/$file
                sed -i "s|\.bash |\.sh |g" $ShellDir/docker/$file
                sed -i "s|#!/bin/sh|#!/usr/bin/env bash|g" $ShellDir/docker/$file
            fi
        fi

        if [ $file == "default_task.sh" ]; then
        aaa=${ShellDir:1}
        aaa=${aaa//\//\\\\\/}
        sed -i "s|\\\\$ShellDir|\\\\/$aaa|g" $ShellDir/docker/$file
        sed -i "s|apk|#apk|g" $ShellDir/docker/$file
        sed -i "s|^cat $ShellDir/docker/docker_entrypoint.sh|#cat $ShellDir/docker/docker_entrypoint.sh|g" $ShellDir/docker/$file
			if [ -n "$ANDROID_ROOT" ]; then
				#sed -i "s|% 12 + 12|% 12|g" $ShellDir/docker/$file
				#sed -i "s|#!/bin/sh|#!/usr/bin/env bash|g" $ShellDir/docker/$file
				#sed -i "s|sh -x|bash -x|g" $ShellDir/docker/docker_entrypoint.sh
				echo android
			fi
        fi
        if [ $file == "auto_help.sh" ]; then
            sed -i "s|\$4,\$5,\$6,\$7|\$1,\$2,\$3,\$4|g" $ShellDir/docker/$file
            # if [ "$HOSTNAME" == "OpenWrt" ]; then
            # sed -i "s|-gk2|-n|g" $ShellDir/docker/$file
            # fi
        fi
    done

    if [ "$HOSTNAME" == "OpenWrt" ]; then
    sed -i 's/npm /#npm /g' $ShellDir/docker/docker_entrypoint.sh 
    fi
    sed -i 's/git /#git /g' $ShellDir/docker/docker_entrypoint.sh 
    chmod +x $ShellDir/docker/docker_entrypoint.sh
    $ShellDir/docker/docker_entrypoint.sh
    mergedListFile="$ShellDir/docker/merged_list_file.sh"
    sed -i "s|. $ShellDir/docker/auto_help.sh export > $ShellDir/logs/auto_help_export.log && node|$shellenv $ShellDir/ajd.sh|g" $mergedListFile
    sed -i "s|docker_entrypoint.sh|$shellenv $ShellDir/ajd.sh entrypoint|g" $mergedListFile
    sed -i "s|sh +x $ShellDir/docker/auto_help.sh|$shellenv $ShellDir/ajd.sh|g" $mergedListFile
    if [ -n "$ANDROID_ROOT" ] || [ "$HOSTNAME" == "OpenWrt" ]; then
    sed -i "s/ |ts //g" $mergedListFile
    sed -i "s|>>|>|g" $mergedListFile
    fi
    crontab $mergedListFile
    if [ -n "$ANDROID_ROOT" ]; then
        cp $MY_GIT/docker/$CUSTOM_LIST_FILE $ShellDir/docker/
		cp $MY_GIT/ajd.sh $ShellDir/
    fi
}

function show_power(){
    power=`su -c "dumpsys battery"|grep level`
    date
    echo $power 
}

function main(){
    if [ "$param" == "collect" ]
    then
    auto_help
    elif [ "$param" == "entrypoint" ]
    then
    run_entrypoint
    elif [ "$param" == "show_power" ]
    then
    show_power
    else
    run_js
    fi
}
main


